﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Threading;

namespace SpeedRun
{
    public class Run : IDisposable
    {
        public RunTypes runType;
        public User user;
        public TimeSpan time;
        public double distance;
        public double speed;
        public double calories;
        public List<System.Device.Location.GeoCoordinate> coordinates;
        protected static XmlSerializer xmlS = new XmlSerializer(typeof(List<Run>));
        public String Note = String.Empty;
        public DateTime TimeStamp;
        public static void SaveHistory(Run currentRun)
        {
            List<Run> runList = new List<Run>();
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists("History.xml"))
                {
                    using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("History.xml", System.IO.FileMode.Open, isf))
                        runList = (List<Run>)xmlS.Deserialize(isfs);
                }

                runList.Add(currentRun);

                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("History.xml", System.IO.FileMode.OpenOrCreate, isf))
                {
                    xmlS.Serialize(isfs, runList);
                }
            }
            
        }


        public static List<Run> RetrieveHistory()
        {
            List<Run> runList = new List<Run>();
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isf.FileExists("History.xml"))
                    return runList;
                else
                {
                    using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("History.xml", System.IO.FileMode.Open, isf))
                    {
                        StreamReader sr = new StreamReader(isfs);
                        MessageBox.Show(sr.ReadToEnd());
                        sr.Close();
                    }
                    using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("History.xml", System.IO.FileMode.Open, isf))
                    {
                        runList = (List<Run>)xmlS.Deserialize(isfs);
                    }


                }
            }

            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("History.xml", System.IO.FileMode.Open, isf))
                {
                    StreamReader sr = new StreamReader(isfs);
                    MessageBox.Show(sr.ReadToEnd());
                    sr.Close();
                }
            }
            return runList;
        }


        public static void SaveAll(List<Run> allRuns)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (allRuns.Count <= 0)
                {
                    isf.DeleteFile("History.xml");
                    return;
                }
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("History.xml", System.IO.FileMode.OpenOrCreate, isf))
                {
                    xmlS.Serialize(isfs, allRuns);
                }
            }
        }

        void System.IDisposable.Dispose()
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            isf.Dispose();
        }


        public static Comparison<Run> comp = delegate(Run r1, Run r2)
        {
            return r1.TimeStamp.TimeOfDay.TotalSeconds.CompareTo(r2.TimeStamp.TimeOfDay.TotalSeconds);
        };

    }


    public enum Environment
    {
        Indoor,
        Outdoor
    }
}
